package cn.ghf.order.service.impl;

import cn.ghf.basic.service.impl.BaseServiceImpl;
import cn.ghf.basic.util.CodeGenerateUtils;
import cn.ghf.basic.util.QuartzJobInfo;
import cn.ghf.order.domain.AdoptOrder;
import cn.ghf.order.domain.OrderAddress;
import cn.ghf.order.mapper.AdoptOrderMapper;
import cn.ghf.order.mapper.OrderAddressMapper;
import cn.ghf.order.service.IAdoptOrderService;
import cn.ghf.pay.domain.PayBill;
import cn.ghf.pay.mapper.PayBillMapper;
import cn.ghf.pay.service.IPayBillService;
import cn.ghf.pet.domain.Pet;
import cn.ghf.pet.mapper.PetMapper;
import cn.ghf.quartz.service.IQuartzService;
import cn.ghf.user.domain.LoginInfo;
import cn.ghf.user.domain.User;
import cn.ghf.user.domain.UserAddress;
import cn.ghf.user.mapper.UserAddressMapper;
import cn.ghf.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.ldap.HasControls;
import javax.xml.ws.Action;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private AdoptOrderMapper adoptOrderMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IQuartzService quartzService;

    @Override
    @Transactional
    public String submit(Map<String, Object> params, LoginInfo login) {
        //获取参数
        Long petId = Long.valueOf(params.get("pet_id").toString()); //宠物id
        Long addressId = Long.valueOf(params.get("address_id").toString());//用户地址id
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString()); //配送方式
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString()); //支付方式

        //1.下架 或者已领养
        Pet pet = petMapper.loadById(petId);
        pet.setState(0);
        //2.关联领养的用户
        User user = userMapper.loadByLoginInfoId(login.getId());
        pet.setUser_id(user.getId());
        //3.更新下架时间
        pet.setOffsaletime(new Date());
        //4.更新pet
        petMapper.update(pet);

        // 生成订单
        //获取orderAdopt
        AdoptOrder adoptOrder = initAdoptOrder(user, pet);
        //生成统一支付单号
        String unionPaySn = CodeGenerateUtils.generateUnionPaySn();
        adoptOrder.setPaySn(unionPaySn);
        adoptOrderMapper.add(adoptOrder);
        //获取orderAddress ，根据用户地址id查询用户地址信息
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress, adoptOrder);
        //保存
        orderAddressMapper.add(orderAddress);

        //创建支付订单并发起支付请求
        //创建支付订单
        PayBill payBill = buildPayBill(payMethod, user, adoptOrder);
        //保存
        payBillMapper.add(payBill);

        //添加定时任务
        //需要提供参数，订单的标识，名称
        QuartzJobInfo info = new QuartzJobInfo();
        HashMap<String, Object> map = new HashMap<>();
        map.put("paySn", unionPaySn);
        info.setParams(map);
        info.setJobName(unionPaySn);
        info.setFireDate(adoptOrder.getLastPayTime());

        quartzService.addJob(info);

        //发起支付请求
        String payData = payBillService.payRequest(payBill);
        return payData;
    }

    @Override
    public AdoptOrder getById(Long businessKey) {
        return adoptOrderMapper.loadById(businessKey);
    }

    /**
     * 创建支付订单
     * @param payMethod
     * @param user
     * @param adoptOrder
     * @return
     */
    private PayBill buildPayBill(Integer payMethod,User user, AdoptOrder adoptOrder) {
        PayBill payBill = new PayBill();
        payBill.setDigest(adoptOrder.getDigest());
        payBill.setMoney(adoptOrder.getPrice());
        payBill.setUnionPaySn(adoptOrder.getPaySn());
        payBill.setState(0);
        payBill.setLastPayTime(new Date(System.currentTimeMillis()+15*50*1000));
        payBill.setPayChannel(Long.valueOf(payMethod));//0 余额 1 支付宝 2 微信 3 银联
        payBill.setBusinessType("business_type_adopt");
        payBill.setBusinessKey(adoptOrder.getId());
        payBill.setUser_id(user.getId());
        payBill.setShop_id(adoptOrder.getShop_id());
        payBill.setNickName(user.getUsername());
        return payBill;
    }

    /**
     * 获取OrderAddress
     * @param userAddress
     * @param adoptOrder
     * @return
     */
    public OrderAddress userAddress2OrderAddress(UserAddress userAddress,AdoptOrder adoptOrder){
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setId(null);
        orderAddress.setOrder_id(adoptOrder.getId());
        orderAddress.setOrderSn(adoptOrder.getOrderSn());
        return orderAddress;
    }

    /**
     * 获取AdoptOrder
     * @param user
     * @param pet
     * @return
     */
    public AdoptOrder initAdoptOrder(User user,Pet pet){
        AdoptOrder adoptOrder = new AdoptOrder();
        adoptOrder.setDigest("[摘要]领养"+pet.getName()+"的订单！");
        adoptOrder.setState(0); //待支付
        adoptOrder.setPrice(pet.getSaleprice()); //待支付
        adoptOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //lastPayTime最后支付时间  当前时间+15分钟
        Date lastPayTime = new Date(System.currentTimeMillis()+15*60*1000);
        adoptOrder.setLastPayTime(lastPayTime);
        adoptOrder.setPet_id(pet.getId());
        adoptOrder.setUser_id(user.getId());
        adoptOrder.setShop_id(pet.getShop_id());
        return adoptOrder;
    }

}
